<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Projekt_model extends CI_Model {
	
	function GetMissions($tid) {
		$q = $this->db->order_by('end asc')
					  ->where('id', $tid)
					  ->get('missions');
		return $q->result();
	}
	
	function GetAllMisions() {
		$q = $this->db->order_by('id asc')
					  ->get('missions');
		return $q->result();
	}

	function AddAssignment($data) {	//tu nie je moc čo komentovať... 
		
		$this->db->query("INSERT INTO mission(content) VALUES ('$data') ");
	
	}

	
	
	function GetAllSolutions() {
		$q = $this->db->order_by('id asc')
					  ->get('solutions');
		return $q->result();
	}
	
	function GetSolutions($mid) {
		

		$q = $this->db->where('mid', $mid)
					  ->get('solutions');

		return $q->result();
	
	}

	function GetOneSolution($sid) {
		

		$q = $this->db->where('id', $sid)
					  ->get('solutions');

		return $q->result();
	
	}	
	
	function GetComments($sid) {
	
		$q = $this->db->where('sid', $sid)
					  ->get('results');

		return $q->result();
	
	}
	
	function add_eval($data) {
	
		$sol_id = $data['sol_id'];
	
		$data1 = array(
		'text'=> $_POST['komentar']
		);
		
		$data2 = array(
			'points'=> $_POST['hodnotenie'],
			'sid' => $sol_id,
			'uid' => $data['user_id'],
			);
		
		$this->db->where('sid',$sol_id)
				 ->insert('results',$data2);
		
		return $this->db->where('sid',$sol_id)
					->update('results',$data1);
		
	}
	
	function publ($vstup) {
		
		$data = array(
		'resulted'=> $vstup['zverejnit']
		);
		return $this->db->where('id',$vstup['mid'])
					->update('missions',$data);
	
	}
	
	
	function GetMyResults($res_data) {
	
		return  $this->db->where('sid', $res_data['sol_id'])
						 ->where('uid', $res_data['user_id'])
						 ->get('results');
	
	}
	
	
	function win($sol_id) {
		
		$data1 = array(
		'win' =>0,

		);
		$data2 = array(
		'win' => 1,

		);
		
		$q = $this->db->where('id', $sol_id)
				 ->get('solutions');
		
		$mid = $q->result();
		$mid = $mid[0]->mid;
		
		$this->db->where('mid', $mid)
				 ->update('solutions',$data1);
					
		return $this->db->where('id', $sol_id)
					->update('solutions',$data2);
					
	}

	function lose($sol_id) {
	
		$data = array(
		'win' => 0,
		);
		
		return $this->db->where('id', $sol_id)
					->update('solutions',$data);
					
	}
	
	function update_time($mid) {
	
		$data = array(
		'start' => $_POST['start'],
		'end' => $_POST['end'],
		);
		
		return $this->db->where('id', $mid)
					->update('missions',$data);
					
	}

	function edit_mission_cont($mid) {
	
		$data = array(
		'content' => $_POST['zadanie']
		);
	
		return $this->db->where('id', $mid)
					->update('missions',$data);
	
	}
	
	function otvor_db() {
		if ($link = mysql_connect('localhost', 'letnaliga', 'SaintsLL')) {
			if (mysql_select_db('letnaliga', $link)) {
				mysql_query("SET CHARACTER SET 'utf8'", $link); 
				return $link;
			} else {
				echo "Nepodarilo sa vybrať databázu!";
				return false;
			}
		} else {
			echo "Nepodarilo sa spojiť s databázovým serverom!";
			return false;
		}
	}
	
	function show_table($year) {
		error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
		$upper = array('Meno tímu');
		$missions = array();
		
		if ($link = $this->otvor_db()) {
			$sql = "SELECT id, name FROM missions WHERE date_format(end,'%Y') = $year AND start < now() AND end < now() AND resulted = 1 ORDER BY end ASC"; 
			$result = mysql_query($sql, $link);
			$num = 0;
			while ($row = mysql_fetch_array($result)) {
				$num = $num + 1;
				array_push($upper, $num);
				array_push($missions, $row['id']);		
			}
		} else {
			echo "Nepodarilo sa spojiť s databázovým serverom!";
		}
		
		array_push($upper, "Spolu");
		
		$users = array();
		
		if ($link = $this->otvor_db()) {
			$sql = "SELECT id FROM users WHERE type = '0' ORDER BY name ASC"; 
			$result = mysql_query($sql, $link);
			$num = 0;
			while ($row = mysql_fetch_array($result)) {		
				array_push($users, $row['id']);
			}
		} else {
			echo "Nepodarilo sa spojiť s databázovým serverom!";
		}
		
		$solutions = array();
		
		foreach ($users as $user) {
			array_push($solutions, array($user));
			foreach ($missions as $mission) {
				if ($link = $this->otvor_db()) {
					$sql = "SELECT id FROM solutions WHERE $user = uid AND $mission = mid"; 
					$result = mysql_query($sql, $link);
					if ($row = mysql_fetch_array($result)) {		
						array_push($solutions[count($solutions)-1], $row['id']);
					} else {
						array_push($solutions[count($solutions)-1], '');	
					}
				} else {
					echo "Nepodarilo sa spojiť s databázovým serverom!";
				}
			}
		}
		
		$wins = array();
		
		foreach ($solutions as $solutionrow) {
			array_push($wins, array());
			foreach ($solutionrow as $solution) {
				if ($link = $this->otvor_db()) {
					$sql = "SELECT win FROM solutions WHERE $solution = id"; 
					$result = mysql_query($sql, $link);
					if ($row = mysql_fetch_array($result)) {					
						array_push($wins[count($wins)-1], $row['win']);
					} else {
						array_push($wins[count($wins)-1], '');
					}
				} else {
					echo "Nepodarilo sa spojiť s databázovým serverom!";
				}		
			}	
		}
		
		$points = array();
		
		foreach ($solutions as $solutionrow) {
			array_push($points, array());
			$num = 0;
			$sumpoint = 0;
			foreach ($solutionrow as $solution) {
				if ($num == 0) {
					$num = 1;	
					if ($link = $this->otvor_db()) {
						$sql = "SELECT name FROM users WHERE $solution = ID";
						$result = mysql_query($sql, $link);
						while ($row = mysql_fetch_array($result)) {		
							array_push($points[count($points)-1], $row['name']);
						}
					} else {
						echo "Nepodarilo sa spojiť s databázovým serverom!";
					}
				} else {		
					$point = 0;	
					$count = 0;					
					if ($link = $this->otvor_db()) {
						$sql = "SELECT points FROM results WHERE $solution = SID"; 
						$result = mysql_query($sql, $link);				
						while ($row = mysql_fetch_array($result)) {		
							$point = $point + $row['points'];
							$count = $count + 1;										
						}
						$point = $point / $count;
						array_push($points[count($points)-1], $point);
						$sumpoint = $sumpoint + $point;
					} else {
						echo "Nepodarilo sa spojiť s databázovým serverom!";
					}
				}
			}
			array_push($points[count($points)-1], $sumpoint);
		}
		
		$sort = array();
		
		foreach ($points as $key => $row) {
			$sort[$key] = $row[count($row)-1];
		}
		array_multisort($sort, SORT_DESC, $points, $wins);
		
		
		
		$result = '<table style="border-collapse: collapse; border: 1px solid black;" border="0px" cellpadding="5px">
		<tr style="font-weight: bold; background-color: #ff6600; border-bottom: 1px solid black;">';		 
		foreach ($upper as $item) {
			$result .= '<td>'.$item.'</td>';
		}
		$result .= '</tr>';	
		for ($x = 0; $x < count($points); $x++) {
			if ($points[$x][count($row)-1] > 0) {
				$result .= "<tr style='border-top: 1px solid black;'>";
				for ($y = 0; $y < count($points[$x]); $y++) {
					if ($points[$x][$y] != '') {
						if ($wins[$x][$y] == 1) {
							$result .= "<td style='background-color: #00ff3f;'>".$points[$x][$y]."</td>";
						} else {
							if ($y == 0) {
								$result .= "<td style='border-right: 1px solid black; font-weight: bold;'>".$points[$x][$y]."</td>";
							} elseif($y == count($points[$x]) - 1) {
								$result .= "<td style='border-left: 1px solid black; font-weight: bold;'>".$points[$x][$y]."</td>";
							} else {
								$result .= "<td>".$points[$x][$y]."</td>";
							}											
						}
					} else {
						$result .= "<td>-</td>";
					}
				}
				$result .= "</tr>";
			}
		}
		
		$result .= "</table>";

		return $result;		
	}

}